/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CharacterWindow.java
 *
 * Created on Jul 27, 2010, 12:24:08 PM
 */

package gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 *
 * @author darek
 */
public class CharacterWindow extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Graphics2D g=null;

    /** Creates new form CharacterWindow */
    public CharacterWindow() {
        initComponents();
        g=(Graphics2D)panel.getGraphics();
        g.setColor(Color.RED);
        g.setStroke(new BasicStroke(3));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        jTextField2 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        pressure = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        panel = new javax.swing.JPanel();
        min = new javax.swing.JTextField();
        max = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        jTextField2.setText("jTextField2");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(650, 50, 0, 0));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 204, 51));

        pressure.setBackground(new java.awt.Color(255, 204, 102));
        pressure.setForeground(new java.awt.Color(102, 0, 0));
        pressure.setText("0.0");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setForeground(new java.awt.Color(102, 0, 0));
        jLabel1.setText("NACISK:");

        panel.setBackground(new java.awt.Color(255, 204, 102));
        panel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 284, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 228, Short.MAX_VALUE)
        );

        min.setBackground(new java.awt.Color(255, 204, 102));
        min.setForeground(new java.awt.Color(102, 0, 0));
        min.setText("0.0");

        max.setBackground(new java.awt.Color(255, 204, 102));
        max.setForeground(new java.awt.Color(153, 0, 0));
        max.setText("0.0");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(102, 0, 0));
        jLabel2.setText("MIN:");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(102, 0, 0));
        jLabel3.setText("MAX:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(panel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(min, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pressure, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                            .addComponent(max, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(pressure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(min, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(max, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        

    /**
    * @param args the command line arguments
    */

    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextField2;
    public javax.swing.JTextField max;
    public javax.swing.JTextField min;
    public javax.swing.JPanel panel;
    public javax.swing.JTextField pressure;
    // End of variables declaration                   
    public BufferedImage newCharacter(ArrayList<Integer[][]> list, ArrayList<Double> pressureList){
        int x0=getMinX(list)[0];
        int y0=getMinY(list)[0];
        int[] minMaxx=getMinX(list);
        int[] minMaxy=getMinY(list);
        panel.repaint();
        BufferedImage bi=new BufferedImage(2*(minMaxx[1]-minMaxx[0]+1),2*(minMaxy[1]-minMaxy[0]+1),BufferedImage.TYPE_BYTE_GRAY);
        for(int i=0;i<2*(minMaxx[1]-minMaxx[0]+1);i++){
            for(int j=0;j<2*(minMaxy[1]-minMaxy[0]+1);j++){
                bi.setRGB(i, j, Color.WHITE.getRGB());
            }
        }
        Graphics2D g2d=bi.createGraphics();
        g2d.setStroke(new BasicStroke(2));
        int index=0;
        for(Integer[][] c: list){
            g2d.setColor(new Color((int)(Color.BLACK.getRGB()*pressureList.get(index++))));
            g2d.drawLine(2*(c[0][0]-x0), 2*(c[0][1]-y0), 2*(c[0][2]-x0), 2*(c[0][3]-y0));
        }
        g.drawImage(bi, 10, 10, rootPane);

        return bi;
    }

    private int[] getMinX(ArrayList<Integer[][]> list) {
        int result[]={list.get(0)[0][0],list.get(0)[0][0]++};
        for(int i=0;i<list.size();i++){
            Integer[][] l=list.get(i);
            if(l[0][0]<result[0])result[0]=l[0][0];
            if(l[0][2]<result[0])result[0]=l[0][2];
            if(l[0][0]>result[1])result[1]=l[0][0];
            if(l[0][2]>result[1])result[1]=l[0][2];
//            i+=4;
        }
        if(result[0]-result[1]==0) result[1]=result[0]++;
        return result;
    }

    private int[] getMinY(ArrayList<Integer[][]> list) {
        int result[]={list.get(0)[0][1],list.get(0)[0][1]++};
        for(int i=0;i<list.size();i++){
            Integer[][] l=list.get(i);
            if(l[0][1]<result[0])result[0]=l[0][1];
            if(l[0][3]<result[0])result[0]=l[0][3];
            if(l[0][1]>result[1])result[1]=l[0][1];
            if(l[0][3]>result[1])result[1]=l[0][3];
//            i+=4;
        }
        if(result[0]-result[1]==0) result[1]=result[0]++;
        return result;
    }


}
